package com.android.BluethootChess.connectivity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import com.android.BluethootChess.util.Utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.graphics.Point;

public class BluetoothServer implements IComunication {
	
	private DataInputStream recv;
	private DataOutputStream send;
	private BluetoothSocket socket;
	private BluetoothServerSocket serverSocket;
	
	public BluetoothServer(){
		BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
		if (adapter!=null){
			UUID myUID=UUID.randomUUID();
			try {
				serverSocket=adapter.listenUsingRfcommWithServiceRecord("Chess", myUID);
				socket=serverSocket.accept();  // daca se doreste multithread se adauga aici un thread care
											   // sa prelucreze acest socket.
				send=new DataOutputStream(socket.getOutputStream());
				recv=new DataInputStream(socket.getInputStream());
				
			} catch (IOException e) {
				Utils.logFile("Eroare la Bluetooth server: "+e);
			}
			finally{
				try {
					serverSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Point[] getPointsFromServer() {
		return null;
	}

	public Point[] receivePoints() {
		return null;
	}

	public String recvString() {
		String result = "";
		try {
			result = recv.readUTF();
		} catch (Throwable e) {
			Utils.logFile("RecvString error: " + e.toString());
			Utils.logFileError(e);
		}

		return result;
	}

	public void sendPoints(Point[] points) {
	}

	public void sendPointsAsString(Point[] points) {
	}

	public void sendString(String string) {
		try {
			Utils.logFile("Se trimite: " + string);
			Utils.logFile("Send_value = " + send);

			send.writeUTF(string.toString());
		} catch (Throwable e) {
			Utils.logFile("SendString error: " + e.toString());
			Utils.logFileError(e);
		}
	}

}
